﻿package
{

	import flash.display.*;
	import flash.events.*;
	import flash.net.*;

	import org.papervision3d.core.*;
	import org.papervision3d.core.proto.*;
	import org.papervision3d.scenes.*;
	import org.papervision3d.objects.*;
	import org.papervision3d.cameras.*;
	import org.papervision3d.materials.*;
	import org.papervision3d.materials.utils.*;
	import org.papervision3d.view.*;
	import org.papervision3d.render.*;
	
	import org.papervision3d.objects.primitives.*;

	public class Circle extends MovieClip
	{
		var CAMERA_HEIGHT = 50.0;
		
		private var _viewport :Viewport3D;
		private var _scene :Scene3D;
		private var _camera :Camera3D;
		private var _renderer :BasicRenderEngine;

		private var _screenWidth:Number;
		private var _screenHeight:Number;
		private var _circleRotate:Number = 0.0;
		private var _rootStage :DisplayObject3D;

		private var _planeMaterial :MovieAssetMaterial;
		private var _shadowMaterial :MovieAssetMaterial;

		private var _mouseLayer:Sprite;

		public function Circle()
		{
			_screenWidth = stage.stageWidth;
			_screenHeight = stage.stageHeight;

			init3D();
			init2D();
			buildScene();
			setListeners();
		}

		private function init3D():void
		{
			//--- create viewport ---
			_viewport = new Viewport3D(stage.stageWidth, stage.stageHeight, true, true);
			addChildAt(_viewport, 0);

			//--- create scene ---
			_scene = new Scene3D();

			//--- create camera ---
			_camera = new Camera3D();
			_camera.z = -300;
			_camera.y = CAMERA_HEIGHT;
			_camera.rotationX = -10;
			_camera.zoom = 50;

			//--- create renderer ---
			_renderer = new BasicRenderEngine();
		}

		private function init2D():void
		{
			_mouseLayer = new Sprite();
			_mouseLayer.alpha = 0.0;
			var g = _mouseLayer.graphics;
			g.beginFill(0x00ffffff);
			g.drawRect(0, 0, _screenHeight, _screenHeight);
			g.endFill();
			addChild(_mouseLayer);
		}

		private function buildScene():void
		{
			//--- create MovieClip material ---
			_planeMaterial = new MovieAssetMaterial("Texture", true, true);
			_planeMaterial.doubleSided = true;
			_shadowMaterial = new MovieAssetMaterial("TextureShadow", true, true);
			_shadowMaterial.doubleSided = true;

			_rootStage = new DisplayObject3D();
			_scene.addChild(_rootStage);

			var i;
			var count = 30;
			var r = 200.0;
			for (i=0; i<count; i++) {
				var plane = new Plane(_planeMaterial, 30, 30);
				var planeShadow = new Plane(_shadowMaterial, 30, 30);

				var rad = (360*i/count) * (Math.PI/180);

				plane.x = r * Math.sin(rad);
				plane.z = r * Math.cos(rad);
				plane.rotationY = 360 * (i / count);

				planeShadow.x = plane.x;
				planeShadow.z = plane.z;
				planeShadow.y = -30.0;
				planeShadow.rotationY = plane.rotationY;

				_rootStage.addChild(plane);
				_rootStage.addChild(planeShadow);
			}

		}

		private function setListeners():void
		{
			addEventListener(Event.ENTER_FRAME, _onEnterFrame);
		}

		private function _onEnterFrame(e:Event):void
		{
			//--- rotate model ---
			_renderer.renderScene(_scene, _camera, _viewport);

			var mx = mouseX;
			var my = mouseY;

			// dx = -1.0(Screen Left) ~ 1.0(Screen Right)
			var halfWidth = _screenWidth / 2;
			var dx = (mx - halfWidth) / halfWidth;

			var newRot = (5.0 * dx);
//			static var curRot = 0;

			_rootStage.rotationY += (5.0 * dx);

			var halfHeight = _screenHeight / 2;
			var dy = 50 * (my - halfHeight) / halfHeight;

			var newY = CAMERA_HEIGHT + dy;
			_camera.y = _camera.y + (newY - _camera.y)/2;
		}

	}	//end of class

}	//end of package
